package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置拦截器
 * 可以让LoginInterceptor拦截器生效。
 *
 */
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
    /**
     * 添加拦截器
     * @param registry
     */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加登录拦截器。 拦截部分请求，优先级低
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginInterceptor());
        // 添加配置可以放行哪些路径
        interceptorRegistration.excludePathPatterns(
                "/shop/**",
                "/voucher/**",
                "/shop-type/**",
                "/upload/**",
                "/blog/hot",
                "/user/code",
                "/user/login"
        ).order(1);
        // 添加刷新拦截所有请求。 设置优先级高于 拦截部分请求
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);

    }
}
